package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.entity.pojo.Categories;
import com.entity.pojo.CategoriesRelation;
import com.mapper.CategoriesRelationMapper;
import com.service.CategoriesRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.CategoriesService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 目录关系表 服务实现类
 * </p>
 *
 * @author chenMo
 * @since 2022-04-11
 */
@Service
public class CategoriesRelationServiceImpl extends ServiceImpl<CategoriesRelationMapper, CategoriesRelation> implements CategoriesRelationService {
    @Resource
    private CategoriesService categoriesService;

    @Override
    public List<Categories> getCanBeAddedNextCategoryList(Integer currentCategoryId,Integer courseId) {
        // 先查该目录已有的关联目录id
        List<Integer> nextCategoryIds = baseMapper.selectList(new QueryWrapper<CategoriesRelation>()
                .eq("categoryId", currentCategoryId))
                .stream()
                .map(CategoriesRelation::getNextCategoryId)
                .collect(Collectors.toList());
        nextCategoryIds.add(currentCategoryId);
        return categoriesService.list(new QueryWrapper<Categories>()
                .notIn("categoryId", nextCategoryIds)
                .eq("courseId",courseId)
                .select("categoryId", "categoryName"));
    }

    @Override
    public boolean addCategoryRelation(CategoriesRelation categoryRelation) {
        baseMapper.insert(categoryRelation);
        return updateNextCategoryStr(categoryRelation.getCategoryId());
    }

    @Override
    public boolean deleteCategoryRelation(Integer categoryId, Integer nextCategoryId) {
        baseMapper.delete(new QueryWrapper<CategoriesRelation>()
                .eq("categoryId", categoryId)
                .eq("nextCategoryId", nextCategoryId));
        return updateNextCategoryStr(categoryId);
    }

    // 根据目录id更新目录表中的关联目录字符串
    private boolean updateNextCategoryStr(Integer categoryId) {
        // 查询该目录的所有关联目录id
        List<Integer> nextCategoryIdList = baseMapper
                .selectList(new QueryWrapper<CategoriesRelation>()
                        .eq("categoryId", categoryId)
                        .select("nextCategoryId"))
                .stream().map(CategoriesRelation::getNextCategoryId)
                .collect(Collectors.toList());
        if (nextCategoryIdList.isEmpty()) {
            // 如果查询结果为空，则直接置空关联目录字符串
            Categories category = categoriesService.getById(categoryId)
                    .setNextCategoryStr("");
            return categoriesService.updateById(category);
        }
        // 如果查询结果不为空，则查询该目录的所有关联目录名称
        List<String> nextCategoryNameList = categoriesService
                .listByIds(nextCategoryIdList)
                .stream().map(Categories::getCategoryName)
                .collect(Collectors.toList());
        // 拼接成字符串，以冒号分割
        String nextCategoryNameStr = String.join(":", nextCategoryNameList);
        // 查找到该目录在目录表中的记录
        Categories category = categoriesService.getById(categoryId)
                .setNextCategoryStr(nextCategoryNameStr);
        return categoriesService.updateById(category);
    }


}
